package com.sys.websocket;

import com.sys.utils.ShiroUtils;
import com.sys.utils.Utils;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.server.ServletServerHttpRequest;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;

import javax.servlet.http.HttpSession;
import javax.websocket.HandshakeResponse;
import javax.websocket.server.HandshakeRequest;
import javax.websocket.server.ServerEndpointConfig;
import java.util.List;
import java.util.Map;

/**
 * 开启WebSocket支持
 *
 * @author com.bhudy
 */
@EnableWebSocket
@Configuration
public class WebSocketConfig extends ServerEndpointConfig.Configurator {

    /**
     * 修改握手,就是在握手协议建立之前修改其中携带的内容
     *
     * @param sec
     * @param request
     * @param response
     */
    @Override
    public void modifyHandshake(ServerEndpointConfig sec, HandshakeRequest request, HandshakeResponse response) {
        sec.getUserProperties().put("userId", ShiroUtils.getUserId());
        super.modifyHandshake(sec, request, response);
    }

    /**
     * 部署在外部tomcat需要删除该方法，不然项目启动不了
     *
     * @return ServerEndpointExporter
     */
    @Bean
    public ServerEndpointExporter serverEndpointExporter() {
        return new ServerEndpointExporter();
    }

}